home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / mint / lib / mntc6846.zoo / patch / frexp.spp < prev    next >
Encoding:
Text File  |  1994-11-14  |  2.5 KB  |  93 lines

  1. ! C68 remove exponent from 8 byte floating point number
  2. !-----------------------------------------------------------------------------
  3. ! ported to 68000 by Kai-Uwe Bloem, 12/89
  4. !  #1  original author: Peter S. Housel 9/21/88,01/17/89,03/19/89,5/24/89
  5. !  #2    added support for denormalized numbers            -kub-, 01/90
  6. !  #3    added code to allow for different sizeof(int) values
  7. !    by using limits.h
  8. !    Added NaN and infinity error cases.            -djw- 09/93
  9. !-----------------------------------------------------------------------------
  10. !  double frexp (double x, int * nptr)
  11. !
  12. !  The function |frexp()| splits a floating point number into fraction |f| and
  13. !  an exponent |n|, such that the absolute value of |f| is less than 1.0 but
  14. !  not less than 0.5, and such that |f| times 2 raised to the power |n| is
  15. !  equal to |x|.  The fraction |f| is returned and as a side effect the
  16. !  exponent |n| is stored into the place pointed to by |nptr|.  If thw
  17. !  argument to |frexp()| is zero then both returned values will be zero.
  18. !
  19. !  If value is NaN, NaN is returned and errno may be set to EDOM.
  20. !
  21. !  If value is an infinity, an implementation defined value is returned, and
  22. !  errno set to EDOM
  23. !-----------------------------------------------------------------------------
  24.  
  25. BIAS8    =    0x3ff - 1
  26.  
  27. #include <errno.h>
  28.  
  29.     .sect .text
  30.  
  31.     .define _frexp
  32.  
  33.     .extern    _errno
  34.  
  35. #ifdef __MSHORT__
  36. #define LN    w
  37. #else
  38. #define LN    l
  39. #endif
  40.  
  41. _frexp:
  42.     move.l    12(sp),a0    ! inialize exponent for loop
  43. #ifndef __MSHORT__
  44.     moveq    #0,d0        ! ensure d0 is clear
  45. #endif
  46.     clr.LN    (a0)
  47. 2:
  48.     lea    4(sp),a1
  49.     cmp.l    #0x7ff00000,(a1) ! Test first part for just max exponent set
  50.     bne    5f
  51.     tst.l    4(a1)        ! Test second part is all zeroes
  52.     beq    infinity
  53. 5:    move.w    (a1),d0        ! extract value.exp
  54.     move.w    d0,d2
  55.     lsr.w    #4,d0
  56.     and.LN    #0x7ff,d0    ! kill sign bit
  57.  
  58.     cmp.w    #0x7ff,d0    ! NaN ?
  59.     beq    NaNval
  60.  
  61.     cmp.LN    #BIAS8,d0    ! get out of loop if finally (a1) in [0.5,1.0)
  62.     beq    3f
  63.  
  64.     and.w    #0x0f,(a1)    ! remove exponent from value.mantissa
  65.     tst.w    d0        ! check for zero exponent - no leading "1"
  66.     beq    0f
  67.     or.w    #0x10,(a1)    ! restore implied leading "1"
  68.     bra    1f
  69. 0:    add.LN    #1,d0
  70. 1:
  71.     move.l    (a1),d1        ! check for zero
  72.     or.l    4(a1),d1
  73.     beq    3f        ! if zero, all done : exp = 0, num = 0.0
  74.  
  75.     sub.LN    #BIAS8,d0    ! remove bias
  76.     move.l    8(a1),a0    ! add current exponent in
  77.     add.LN    d0,(a0)
  78.  
  79.     move.LN    #BIAS8,d0    ! set bias for return value
  80.     clr.w    d1        ! rounding = 0
  81.  
  82.     jsr    .Xnorm8        ! normalize result
  83.     bra    2b        ! loop around to catch denormalized numbers
  84. 3:
  85.     movem.l    4(sp),d0-d1    ! return normalized mantissa
  86.     rts
  87.  
  88. NaNval:
  89. infinity:
  90.     move.LN    #EDOM,_errno    ! set errno
  91.     movem.l    (a1),d0-d1    ! copy across input as reply
  92.     rts
  93.